package web;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

import beans.News;

import dao.NewsDAO;

@Controller
public class EditNewsController {
	
	private NewsDAO newsDAO;

	public void setNewsDAO(NewsDAO newsDAO) {
		this.newsDAO = newsDAO;
	}
	
	@RequestMapping(value = "/deletenews={newsId}", method=RequestMethod.GET)
	public ModelAndView adminnews(@PathVariable("newsId") int id, HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		News news = newsDAO.findNewsById(id);
		newsDAO.deleteNews(news);
		return new ModelAndView("redirect:adminnews.htm#box-two");
	}
	@RequestMapping(value = "/editnews={newsId}", method=RequestMethod.GET)
	public ModelAndView editnews(@PathVariable("newsId") int id, HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		News editedNews = newsDAO.findNewsById(id);
		ModelMap modelMap = new ModelMap();
		modelMap.addAttribute("editedNews", editedNews);
		return new ModelAndView("editnews", modelMap);
	}
	@RequestMapping(value = "/adminnewsedit={newsId}", method=RequestMethod.POST)
	public ModelAndView adminuseredit(@PathVariable("newsId") int id, HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		String title = request.getParameter("title");
		String newss = request.getParameter("news");
		News news = newsDAO.findNewsById(id);
		news.setTitle(title);
		news.setNews(newss);
		newsDAO.UpdateNews(news);
		return new ModelAndView("redirect:adminnews.htm#box-two");
	}
}
